package org.sample.aaatest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private TestService testService;

    @Autowired
    private TestSingle testSingle;

    @GetMapping("/getById")
    public ResponseEntity<Object> getUserById(@RequestParam(name = "id") int id) {
        System.out.println("TestController\ttestSingle=" + testSingle.getClass().getName());
        TestBean test = testService.findTestById(id);
        System.out.println("TestController\tgetUserById:test=" + test);
        return new ResponseEntity<>(test, HttpStatus.OK);
    }

    @GetMapping("/count")
    public ResponseEntity<Object> countUser() {
        long count = testService.count();
        System.out.println("TestController\tcount:" + count);
        return new ResponseEntity<>(count, HttpStatus.OK);
    }

    @GetMapping("/getAll")
    public ResponseEntity<Object> getAll() {
        List<TestBean> all = testService.getAll();
        System.out.println("TestController\tgetAll:all=" + all);
        return new ResponseEntity<>(all, HttpStatus.OK);
    }

    @GetMapping("/add")
    public ResponseEntity<Object> addUser(@RequestParam Integer id, @RequestParam String name) {
        System.out.println("TestController\taddUser:userId=" + id + ",name=" + name);
        System.out.println("TestController\ttestSingle=" + testSingle.getClass().getName());
        TestBean user = new TestBean(id, name);
        testService.addUser(user);
        return new ResponseEntity<>("success", HttpStatus.OK);
    }

    @GetMapping("/delete")
    public ResponseEntity<Object> deleteUser(@RequestParam int userId) {
        System.out.println("TestController\tdeleteUser:userId=" + userId);
        testService.deleteUser(userId);
        return new ResponseEntity<>("success", HttpStatus.OK);
    }
}
